<script lang="ts">
import { defineComponent,ref,} from "vue";
import { useRouter } from "vue-router";
import { useRoute } from "vue-router";
import { getNotesdetail } from "../api/hand"
import {getHandComment} from "../api/hand"
import { onMounted } from "vue"
import {onBeforeMount} from "vue"
import {getimage } from "../hooks/image"
import {playerTime } from "../hooks/filters"
import { storeToRefs } from 'pinia';
import { useCounterStore } from '../stores/home'
import { showToast } from 'vant'




export default defineComponent({
    setup(){
       
        let router:any = useRouter()
        let route:any = useRoute()

        const store = useCounterStore()
        const {commentlikearr,idarr,tocked,userid,userInfoheard} = storeToRefs(store)
        // 作为 action 的 increment 可以直接解构
        const {uncommentFuns,commentFuns,incrementfalse,increment } = store;

        let noteid:any=ref(null);

        let istoken=localStorage.getItem('token')!=null;
    console.log('istoken=>',istoken)

        const show = ref(false);
     
        //   获取手记详细信息
        noteid.value=route.query.id;  
        let id=noteid.value;
        let Notesdetail:any= ref([])  
         let toHandWite = () => {
            router.push({path:"/handwrite",
            query:{
                id:noteid.value,
                length:HandComment.value.length + newCommentList.value.length
            }
        })
        // console.log()
        }
        // let cover:any = ref(null)
        let getNotesdetailFun = (id:any)=> {
            getNotesdetail(id).then((data:any) =>{
                // console.log("getNotesdetail=>",data)
                Notesdetail.value=data.data
                console.log("Notesdetail=>",Notesdetail)
                // console.log("1111",Notesdetail.value.article.cover)
            //  cover.value = Notesdetail.value.article.cover;
            //  console.log("cover=>",cover.value);

            })
            

        };
        // 获取手记详情评论
        let HandComment:any=ref([])
       let getHandCommentFun = (id:any) =>{
        getHandComment(id).then((data:any) =>{
            // console.log("getHandComment=>",data)
            HandComment.value = data.data
            console.log("HandComment=>",HandComment)

        })
       }

      //遮罩层
      let bigpic:any = ref(null)
      let pic = (index:number)=>{
        // console.log("Notesdetail.images=>",Notesdetail.value.images)
        // console.log("index",index)
        Notesdetail.value.images.forEach((item1:any,index1:any)=>{
            if(index == index1){
                bigpic.value= Notesdetail.value.images[index1]

            }
            // console.log("bigpic=>",bigpic.value)
        })
      }

       //   发评论
      let mycommentid = 1234
      let commentToOthers:any = ref(null) //双向绑定我的发的评论内容
      let localArr=JSON.parse(window.localStorage.getItem("myComment"))
      let newHandComment:any = ref(localArr||[]) //新数组
      let newCommentList:any =ref([]) //过滤了id的新数组
      // 发评论的方法
      let sendComment = () =>{
        if (tocked.value!==null) {
            mycommentid+=1
        // 获取当前发布评论的时间
        let currentTime = new Date().getTime()

        // 评论长度加一
        // HandComment.length+=1

        // 新数组
        localArr = JSON.parse(window.localStorage.getItem("myComment"))
        newHandComment = ref(localArr||[])
        newHandComment.value.push( 
            {
            clicknoteId:noteid.value,
            id:mycommentid, //评论id
            contentText:commentToOthers.value, //评论的内容
            likeCount:0,  //评论点赞数
            createTimeUnix:currentTime,// 发评论的时间戳
            userDetail:{
                id:userid,  //发评论的人的id
                // avatar_small:userInfoheard.avatar, //发评论的人的头像
                // nickname:userInfoheard.username  //发评论的人的昵称
            }

        }
     )
      // 输入完内容赋空值
      commentToOthers.value = '' 
      
        // 过滤手记的id
        newCommentList.value = newHandComment.value.filter((item: any) => {
            return item.clicknoteId == noteid.value
        })
        console.log("newCommentList=>", newCommentList)


        // 按照最新时间排序
        newCommentList.value.sort(function (a: any, b: any) {
            return b.createTimeUnix - a.createTimeUnix
        })
      window.localStorage.setItem("myComment", JSON.stringify(newCommentList.value))
        } else{
         
         showToast('请登录');
             router.push({
                 path: '/register',
             });
 
       }
        




  }

        // 过滤手记的id
        newCommentList.value = newHandComment.value.filter((item: any) => {
            return item.clicknoteId == noteid.value
        })
        console.log("newCommentList=>", newCommentList)


        // 按照最新时间排序
        newCommentList.value.sort(function (a: any, b: any) {
            return b.createTimeUnix - a.createTimeUnix
        })
        window.localStorage.setItem("myComment", JSON.stringify(newCommentList.value))
         // 删除我发的评论
        let delmycomment = (index: any) => {
        newCommentList.value.splice(index, 1);
        console.log('21111111111=>',newCommentList.value)
        window.localStorage.setItem("myComment", JSON.stringify(newCommentList.value))
        localArr = JSON.parse(window.localStorage.getItem("myComment"));
        console.log('3333333333333333', localArr);
        }



       

       

        onBeforeMount(() => {
            getNotesdetailFun(id);
            getHandCommentFun(id)
            localArr = JSON.parse(window.localStorage.getItem("myComment"))

        })
        return{
            toHandWite,
            Notesdetail,
            HandComment,
            getimage, //图片处理
            playerTime,//发布时间处理
            show,
            bigpic,
            pic,
            userInfoheard,
            userid,
            sendComment, //发评论的方法
            newHandComment,  //新数组
            commentToOthers,  //双向绑定的发评论的内容
            newCommentList, //过滤后的评论数组
            commentlikearr,
            uncommentFuns,
            commentFuns,
            idarr,
            incrementfalse,
            increment,
            delmycomment,
            istoken

        }
    }

})

</script>


<template>
    <div class="hand-detail" v-if="Notesdetail.article">
        <!-- 顶部的头像等.... -->
        <div class="hand-detail-top">
            <span class="iconfont icon-xiangzuojiantou" @click="toHandWite"></span>

            <div class="top-center" v-if="Notesdetail.user"> 
                 <div class="center-name"> 
                    <img :src="Notesdetail.user.avatar" >
                    <span>{{Notesdetail.user.username}}</span>
                    <span class="iconfont icon-VIP1" style="color:red" v-if="Notesdetail.user.vip_flag==1"></span>
                 </div>
                
                <p v-if="!idarr.includes(Notesdetail.user.id)||!istoken" @click="increment(Notesdetail.user.id)">+ 关注</p>
                <p  v-else @click="incrementfalse(Notesdetail.user.id)">已关注</p>

            </div>
            <span class="iconfont icon-fenxiang2"></span>
        </div>

        <div class="nulldiv" style="height:40px"></div>

        <!-- 内容和图片 -->
        <div class="hand-detail-content">
            <!-- 图片 -->
            <div class="img-box">
                <!-- <div class="imgs" v-for="(item,index) in Notesdetail.images" :key="index">
                    <img :src="item ">
                </div> -->

                    <!-- 遮罩层级 -->
                    <van-button class="imgs" type="primary" v-for="(item, index) in Notesdetail.images" :key="index" @click="show = true; pic(index)">
                            <img :src="getimage(item + '?params50y50')">
                    </van-button>
                            
                    <van-overlay :show="show" @click="show = false">
                        <div class="wrapper" @click.stop>
                            <div class="block" @click="show = false" >
                                <img :src="bigpic">
                            </div>
                        </div>
                    </van-overlay> 
            </div>
            <!-- 文字 -->
            <div class="hand-detail-words">
                
                <!-- 标题 -->
                <h3>{{Notesdetail.title}}</h3>
                <!-- 内容 -->
                <p>
                    {{ Notesdetail.content }}
                </p>

                <div class="hand-detail-words-tag" v-if="Notesdetail.topics">
                  # {{ Notesdetail.topics[0].name}}
                </div>
                <!-- 视频 -->
                <div class="hand-detail-video" v-if="Notesdetail.article.id">
                    <div class="hand-detail-video-pic" >
                        <img :src="getimage(Notesdetail.article.cover+'params50y50')">
                    </div>
                    <div class="hand-detail-video-pic-worlds">
                        <p class="video-name">{{Notesdetail.article.title}}</p>
                        <p class="video-author">@{{ Notesdetail.article.author.userinfo.username }}</p>
                    </div>

                </div>
                <div class="time">
                    {{ playerTime(Notesdetail.addtime*1000)  }} · {{ Notesdetail.ip_location}}
                </div>

            </div>
        </div>
            <!-- 评论 -->
        <div class="hande-detail-comment">
            <div class="comment-top">
                共 {{HandComment.length + newCommentList.length}} 条评论
            </div>
            <!-- 发评论的框框 -->
            <div class="send-comment">
                <img src="https://cs.xinpianchang.com/uploadfile/user/2017/04/05/58e4be784ff29.jpeg">
                <input type="text" placeholder="说说你的看法吧~" v-model="commentToOthers" @keyup.enter="sendComment"> 

            </div>

            <!-- 我发的评论 -->
            <div class="others-comment-box" v-if="newCommentList.length" >
            <van-swipe-cell class="others-comment" v-for="(item, index) in newCommentList" :key="index">
            <!-- <div class="others-comment"  v-for="(item,index) in newCommentList" :key="index"> -->
                <!-- 用户头像 -->
                <div class="comment-pic" v-if="item.userDetail">
                     <img :src="userInfoheard.avatar">
                </div>
               
                <div class="comment-content" v-if="item.userDetail">
                    <div class="nickname">
                        <p class="others-name">{{userInfoheard.username}}
                             <!-- <span class="iconfont icon-VIP1"></span> -->
                            </p>
                        <p class="others-worlds">{{item.contentText}}</p>
                    </div>
                    <!-- <div>
                        <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                        <span class="iconfont icon-dianzan1"></span>
                    </div> -->
                        <!-- 点赞不红 -->
                    <div v-if="!commentlikearr.includes(item.id)||!istoken">
                        <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount}}</em>
                          <span class="iconfont icon-dianzan" @click.stop="commentFuns(item.id)"></span>
                        </div>
                        <!-- 点了赞变红了 -->
                        <div v-else>
                            <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount+ 1 }}</em>
                          <span class="iconfont icon-dianzan_kuai" style="color:red" @click.stop="uncommentFuns(item.id)"></span>
                        </div> 

                    
                    </div>
                <!-- </div> -->
                <template #right>
                    <van-button square type="danger" text="删除" @click="delmycomment(index)" />
                  </template>
                </van-swipe-cell>
                
             </div>

            <!-- 其他用户发的评论 -->
            <div class="others-comment-box" v-if="HandComment.length" >
            <div class="others-comment"  v-for="(item,index) in HandComment" :key="index">
                <!-- 用户头像 -->
                <div class="comment-pic" v-if="item.userDetail">
                     <img :src="item.userDetail.avatar_small">
                </div>
               
                <div class="comment-content" v-if="item.userDetail">
                    <div class="nickname">
                        <p class="others-name">{{item.userDetail.nickname}} 
                            <!-- <span class="iconfont icon-VIP1"></span> -->
                        </p>
                        <p class="others-worlds">{{item.contentText}}</p>
                    </div>
                    <!-- <div>
                        <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                        <span class="iconfont icon-dianzan1"></span>
                    </div> -->
                    <!-- 点赞不红 -->
                        <div v-if="!commentlikearr.includes(item.userDetail.id)||!istoken">
                            <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount }}</em>
                              <span class="iconfont icon-dianzan" @click.stop="commentFuns(item.userDetail.id)"></span>
                        </div>
                        <!-- 点了赞变红了 -->
                        <div v-else>
                            <em style="margin-right: 3px; font-size: 12px;">{{ item.likeCount + 1 }}</em>
                            <span class="iconfont icon-dianzan_kuai" style="color:red" @click.stop="uncommentFuns(item.userDetail.id)"></span>
                        </div> 
                    
                </div>
            </div>
        </div>

            <!-- 如果没有评论的话 -->
        <div class="null-comment" v-if="newCommentList.length==0&&HandComment.length==0">
            <p>暂无评论,说说你的看法吧</p>
        </div>
        </div>
    </div>
</template>

<style lang="scss">
.van-overlay{
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0,0,0 ,0.8)!important;
}
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    // height: 100%;
  }

  .block {
    // width: 120px;
    // height: 120px;
    background-color: #fff;
    // position: relative;
    height: 100%;
    display: flex;
    align-items: center;
    img{
        // position: absolute;
        // top: 135px;
        // left: -188px;
        width: 375px;
        height: auto;
    }
  }
*{
    padding: 0;
    margin: 0;

}
.hand-detail{
    .hand-detail-top{
        padding-top: 10px;
        width: 100%;
        height: 42px;
        padding-left: 16px;
        padding-right: 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 10px;
        border-bottom: 2px solid #f6f6f6;
        position: fixed;
        z-index: 999;
        background-color: #ffffff;
        .icon-xiangzuojiantou{
            font-size: 20px;
            font-weight: bolder;
            width: 3%;
        }
        .icon-fenxiang2{
            font-size: 25px;
            width: 5%;
            margin-left: 5%;

        }
        .top-center{
            flex: 1;
            display: flex;
            justify-content: space-between;
            margin-left: 6%;
            align-items: center;

            .center-name{
                display: flex;
                align-items: center;
                img{
                width: 32px;
                height: 32px;
                border-radius: 50%;
                margin-right: 10px;
            }
            }
            p{
                width: 65px;
                height: 25px;
                border-radius: 20px;
                color: #cd4453;
                border: 2px solid #cd4453;
                line-height: 27px;
                text-align: center;
                font-size: 12px;
            }
        }
    }
    .hand-detail-content{
        padding-left: 16px;
        padding-right: 16px;
        box-sizing: border-box;
        padding-top: 15px;
        border-bottom: 2px solid #f7f7f7;
        .img-box{ 
            display: flex;
            flex-wrap: wrap;
            // justify-content: space-between;
            padding-bottom: 20px;
            // margin-bottom: 20px;
            .imgs{
                width: 110px;
                height: 110px;
                border-radius: 3px;
                overflow: hidden;
                margin-bottom: 10px;
                background-color: #aba9b1;
                border:none;
                padding: 0px;
                margin-right: 5px;
                &:nth-of-type(3), &:nth-of-type(6), &:nth-of-type(9){
                    margin-right: 0px;
                }
                .van-button__content{
                    width: 110px;
                    height: 110px;
                    .van-button__text{
                        width: 110px;
                        height: 110px;
                    }
                }
                img{
                    width: 100%;
                    height: 100%;
                }
            }
        }
        .hand-detail-words{
            // margin-top: 20px;
           
            padding-bottom: 16px;
            h3{
                font-weight: bold;
                margin-bottom: 10px;
            }
            .hand-detail-words-tag{
                color: #466eb6;
                margin-top: 5px;
            }
            .hand-detail-video{
                display: flex;
                .hand-detail-video-pic{
                    width: 160px;
                    height: 92px;
                    border-radius: 5px 0px 0px 5px;
                    overflow: hidden;
                    img{
                        width: 100%;
                        height: 100%;
                    }
                }
                .hand-detail-video-pic-worlds{
                    width: 184px;
                    height: 92px;
                    border-radius: 0px 5px 5px 0px;
                    background-color: #f3f4f6;
                    padding: 14px;
                    box-sizing: border-box;

                    .video-name {
                            font-size: 13px;
                            -webkit-line-clamp: 2;
                            display: -webkit-box;
                            -webkit-box-orient: vertical;
                            overflow: hidden;
                            text-overflow: ellipsis;
                        }

                        .video-author {
                            margin-top: 12px;
                            color: #95969b;
                            font-size: 12px;
                        }


                }
            }
            .time{
                color: #aba9b1;
                font-size: 11px;
                margin-top: 10px;
            }
        }
    }
    .hande-detail-comment{ 
        
        width: 100%;
        height: 500px;
        padding-left: 16px;
        box-sizing: border-box;
        .comment-top{
            font-size: 14;
            color: #525251;
            margin-top: 18px;
           
        }
        .send-comment{
            display: flex;
            padding-right: 16px;;
            margin-top: 20px;

            img{
                width: 35px;
                height: 35px;
                border-radius: 50%;
                margin-right: 11px;
                display: none;
            }
            input{
                flex: 1;
                outline: none;
                border: none;
                height: 37px;
                background-color: #f3f4f6;
                border-radius: 20px;
                text-indent: 20px;
                font-size: 14px;

            }
        }
        .others-comment-box{
          .others-comment{
            .van-swipe-cell__wrapper{
                width: 100%;
                display: flex;
            }
             .van-button{
                height: 100%;
                }
            display: flex;
            margin-top: 20px;
            .comment-pic{
                img{
                    width: 35px;
                    height: 35px;
                    border-radius: 50%;
                    margin-right: 10px;
                }
            }

            .comment-content{
                display: flex;
                flex: 1;
                border-bottom: 2px solid #f7f7f7;
                justify-content: space-between;
                padding-bottom: 16px;
                .nickname{
                    .others-name{
                        color: #98979d;
                        font-size: 13px;
                        .icon-VIP1{
                            color: red;
                        }
                    }
                    .others-worlds{
                        margin-top: 9px;
                    }

                }
                span{
                    margin-right:16px ;
                }
            }
        }

        }
        .null-comment{
            margin-top: 20px;
            text-align: center;
            p{
                text-align: center;
                font-size: 14px;
                color: #98979d;
            }
        }
      
    }
}

   


</style>